﻿using System;
using System.Collections.Generic;
using Goalchee.Common;
using Goalchee.DataModel;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace Goalchee
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class GoalsPerson : LayoutAwarePage
    {
        private readonly DataServiceCall _dataService = new DataServiceCall ( );
        private GoalData firstGoalDetail;

        public GoalsPerson ( )
        {
            this.InitializeComponent ( );
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState ( Object navigationParameter, Dictionary<String, Object> pageState )
        {
            //var queryText = navigationParameter as String;
            //if ( string.IsNullOrEmpty ( queryText ) )
            //    return;
            //else
            //    pageSubTitle.Text = queryText;
            //var sampleDataGroups = _dataService.GetSearchGoalsForTag ( ( string ) navigationParameter );
            //firstGoalDetail = ReadFirstGoal ( _dataService.ReadSearchGoalsFromTags ( sampleDataGroups ) );

            firstGoalDetail = ( GoalData ) navigationParameter;
            var personsData = _dataService.GetPersonByGoals ( firstGoalDetail.GoalId );
            RenderPage ( _dataService.ReadPersonByGoals ( personsData ) );
        }

        private GoalData ReadFirstGoal ( List<GoalData> readSearchGoalsFromTags )
        {
            GoalData goalItem = new GoalData
                                    {
                                        GoalId = readSearchGoalsFromTags [ 0 ].GoalId,
                                        Name = readSearchGoalsFromTags [ 0 ].Name,
                                        NumRegisteredPeople = readSearchGoalsFromTags [ 0 ].NumRegisteredPeople,
                                        Link = readSearchGoalsFromTags [ 0 ].Link
                                    };
            return goalItem;
        }

        private void RenderPage ( List<Person> personsData )
        {
            pageSubTitle.Text = string.Format ( "number of people who want to {0} are {1}", firstGoalDetail.Name.ToUpper ( ), firstGoalDetail.NumRegisteredPeople );
            int count = 0;
            string profileImageUrl = string.Empty;
            foreach ( Person person in personsData )
            {
                //stackpanel
                StackPanel stackPanel = new StackPanel
                                            {
                                                Width = double.Parse ( "120" ),
                                                Margin = new Thickness ( 10, 0, 0, 60 ),
                                                AllowDrop = false,
                                                Background = new SolidColorBrush ( Colors.Black )
                                            };
                StackPanel textStackpanel = new StackPanel
                                                {
                                                    Width = double.Parse ( "120" ),
                                                    Margin = new Thickness ( 0, 0, 0, 0 ),
                                                    AllowDrop = false,
                                                    Background = new SolidColorBrush ( Colors.Gray )
                                                };



                //image and bitmapimage for the source for profile image
                count++;
                profileImageUrl = string.IsNullOrEmpty ( person.ProfileImageUrl ) ? "http://acf.43things.com/images/profile/i_new_user.gif" : person.ProfileImageUrl;
                BitmapImage img = new BitmapImage { UriSource = new Uri ( profileImageUrl ) };

                Image image = new Image
                                  {
                                      Name = string.Format ( "{0}{1}", person.PersonName, count ),
                                      Source = img,
                                      Stretch = Stretch.UniformToFill,
                                      Height = Double.Parse ( "120" ),
                                      Width = Double.Parse ( "120" ),
                                      //Margin = new Thickness ( 20, 0, 18, 0 )
                                  };

                //textblock name
                TextBlock subtitle = new TextBlock
                {
                    Text = string.Format ( "{0}", person.PersonName ),
                    Margin = new Thickness ( 5, 5, 0, 0 ),
                    Height = double.Parse ( "30" ),
                    FontSize = double.Parse ( "16.667" )
                };
                //subtitle.SetResourceReference ( StyleProperty, "PageHeaderTextStyle" );

                //textblock description
                TextBlock description = new TextBlock
                                            {
                                                Text = string.Format ( "Open Goals : {0}", person.OpenGoalsNumber ),
                                                Margin = new Thickness ( 5, 5, 0, 0 ),
                                                Style = ( Style ) ( this.FindName ( "BodyTextStyle" ) ),
                                                Height = double.Parse ( "10" ),
                                                FontSize = double.Parse ( "8" )
                                            };

                textStackpanel.Children.Add ( subtitle );
                textStackpanel.Children.Add ( description );


                stackPanel.Children.Add ( image );
                stackPanel.Children.Add ( textStackpanel );

                itemsOne.Items.Add ( stackPanel );
            }
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState ( Dictionary<String, Object> pageState )
        {
        }
    }
}